package com.auto.api.core.entity;

import com.auto.api.core.constants.MappingConstants;
import com.auto.api.core.type.TypeFactory;
import java.beans.PropertyDescriptor;
import java.util.Map;
import lombok.Data;
import lombok.ToString;
import org.springframework.core.ResolvableType;

/**
 * @program: 属性上下文
 * @description:
 * @author: liangshf
 * @create: 2022-05-10
 */
@Data
@ToString
public class PropertyContext {

    /**
     * 封装Class对应的所有泛型信息的类型
     */
    private ResolvableType classResType;

    /**
     * 封装属性对应的所有泛型信息的类型
     */
    private ResolvableType propResType;

    /**
     * 封装属性泛型对应的所有泛型信息的类型
     */
    private ResolvableType genPropResType;

    /**
     * 属性编辑器
     */
    private PropertyDescriptor prop;

    /**
     * 属性类型全类名
     */
    private String propType;

    /**
     * 属性泛型类型全类名
     */
    private String propGenType;

    /**
     * 属性类型
     */
    private String propSimpleType;

    /**
     * 属性泛型类型
     */
    private String propSimpleGenType;

    /**
     * 判断属性类型是否集合类型
     * @return
     */
    public boolean isCollectionType(){
        return TypeFactory.collectionType.contains(getPropSimpleType());
    }

    /**
     * 判断是否对象解析类型
     * @return
     */
    public boolean isObjParse(){
        return isCollectionType() || getPropResType().isAssignableFrom(Map.class);
    }

    public String getPropSimpleGenType() {
        if (genPropResType.resolve() == null) {
            return MappingConstants.INT;
        }
        return genPropResType.resolve().getSimpleName();
    }

    public String getJudgePropType() {
        if (genPropResType == null) {
            if (propResType.resolve() == null) {
                return MappingConstants.INT;
            }
            return propResType.resolve().getSimpleName();
        }
        if (genPropResType.resolve() == null) {
            return MappingConstants.INT;
        }
        return genPropResType.resolve().getSimpleName();
    }

    public void setGenPropResType(ResolvableType genPropResType) {
        this.genPropResType = genPropResType;
        if (genPropResType == null) {
            return;
        }
        if (genPropResType.resolve() == null) {
            propGenType = MappingConstants.INT;
            propSimpleGenType = MappingConstants.INT;
            return;
        }
        propGenType = genPropResType.resolve().getName();
        propSimpleGenType = genPropResType.resolve().getSimpleName();
    }



}